package com.app.business.web.business;

import com.app.business.calculate.CalculateHandInMoneyUtils;
import com.app.business.calculate.CalculateUtils;
import com.app.business.en.Status;
import com.app.business.model.*;
import com.app.business.service.*;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.WebUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.AjaxException;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 提前交房申请
 * Created by ddz on 2022/11/24.
 *
 * @author ddz
 */
@Controller
@RequestMapping("/business/advanceTakeHouse/*")
@SuppressWarnings("all")
public class AdvanceTakeHouseController {

    @Autowired
    private ContractService contractService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private ZsProjectService zsProjectService;
    @Autowired
    private HandInMoneyService handInMoneyService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private CarLotService carLotService;
    @Autowired
    private StorageRoomService storageRoomService;
    @Autowired
    private ActionHistoryService actionHistoryService;
    @Autowired
    private BuyHousePersonService buyHousePersonService;

    /**
     * 提前交房申请管理列表
     */
    @RequestMapping("list")
    @RequiresPermissions("advanceTakeHouse:list")
    public void list() {
    }

    /**
     * 加载财务复核数据管理列表
     *
     * @return
     */
    @RequestMapping("loadAdvanceTakeHouseList")
    @ResponseBody
    @RequiresPermissions("advanceTakeHouse:loadAdvanceTakeHouseList")
    public Map loadAdvanceTakeHouseList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<Contract> contractPageBean = this.contractService.getChoosedHousePageBean();
        List<Contract> contractList = contractPageBean.getDatas();
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> checkMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> settleMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        if (!CollectionUtils.isEmpty(contractList)) {
            Map<String, Object> item = null;
            for (Contract contract : contractList) {
                item = null;
                item = Maps.newHashMap();
                item.put("id", contract.getId());
                item.put("town", StringUtils.dataDictShow(townMap.get(contract.getZsProject().getTown())));
                item.put("zsProjectName", contract.getZsProject().getProjectName());
                item.put("contractCode", StringUtils.getNotNull(contract.getContractCode()));
                item.put("name", StringUtils.getNotNull(contract.getName()));
                item.put("chooseHouseNum", StringUtils.getNotNull(contract.getChooseHouseNum()));
                item.put("limitHouseAreaTotal", BigDecimalUtils.getNotNullStr(contract.getLimitHouseAreaTotal()));
                item.put("financeCheckStatus", IntegerUtils.getNotNull(contract.getFinanceCheckStatus()));
                item.put("financeCheckStatusStr", StringUtils.dataDictShow(checkMap.get(IntegerUtils.getNotNull(contract.getFinanceCheckStatus()).toString())));
                item.put("azSignStatus", IntegerUtils.getNotNull(contract.getAzSignStatus()));
                item.put("azSignStatusStr", StringUtils.dataDictShow(signMap.get(IntegerUtils.getNotNull(contract.getAzSignStatus()).toString())));
                item.put("financeSettleStatus", IntegerUtils.getNotNull(contract.getFinanceSettleStatus()));
                item.put("financeSettleStatusStr", StringUtils.dataDictShow(settleMap.get(IntegerUtils.getNotNull(contract.getFinanceSettleStatus()).toString())));
                listData.add(item);
            }
        }
        return LayUiUtils.page(contractPageBean.getTotalCount().intValue(), listData);
    }


    /**
     * 提前交房申请打印页面
     * 补充协议打印
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("advanceTakeHousePrint")
    @RequiresPermissions("advanceTakeHouse:advanceTakeHousePrint")
    public void advanceTakeHousePrint(HttpServletRequest request, ModelMap modelMap) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        int contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null, "数据有误");
        List<House> houseList = new ArrayList<>();
        List<CarLot> carLotList = new ArrayList<>();
        List<StorageRoom> storageRoomList = new ArrayList<>();
        houseList = this.houseService.getHouseListByContractId(contract.getId());
        carLotList = this.carLotService.getCarLotListByContractId(contract.getId());
        storageRoomList = this.storageRoomService.getStorageRoomListByContractId(contract.getId());

        Map<String, List<Map>> buyHousePersonHouseMap = this.buyHousePersonService.getBuyHousePersonByHouse();
        Map<String, List<Map>> buyHousePersonCarLotMap = this.buyHousePersonService.getBuyHousePersonByCarLot();
        Map<String, List<Map>> buyHousePersonStorageRoomMap = this.buyHousePersonService.getBuyHousePersonByStorageRoom();

        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("houseList", houseList);
        modelMap.addAttribute("carLotList", carLotList);
        modelMap.addAttribute("storageRoomList", storageRoomList);
        modelMap.addAttribute("buyHousePersonHouseMap", buyHousePersonHouseMap);
        modelMap.addAttribute("buyHousePersonCarLotMap", buyHousePersonCarLotMap);
        modelMap.addAttribute("buyHousePersonStorageRoomMap", buyHousePersonStorageRoomMap);
        CalculateUtils.main(contract.getId());

    }


    /**
     * 提前交房申请保存
     *
     * @param request
     */
    @RequestMapping("setTransitionMoney")
    @ResponseBody
    @RequiresPermissions("finance:setTransitionMoney")
    public synchronized Response setTransitionMoney(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");
        WebUtils.bind(request, contract);
        this.contractService.save(contract);
        CalculateUtils.main(contract.getId());
        return new Response("0001", "保存成功!");
    }
}